﻿using AutoMapper;
using EFSQLiteOneToManay.AutoMapper;
using EFSQLiteOneToManay.ModelDTO;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Configuration;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.Validation;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Transactions;
using System.Diagnostics;

namespace EFSQLiteOneToManay
{
    class Program
    {
        const string BEACHID = "XL001";
        const string MACHINEID = "API001";
        const string MODELID = "Model01";
        const string PANELID = "Code";
        static void Main(string[] args)
        {
            Mapper.Initialize(MappingConfiguration.Configure);

            #region 写入
            var record = (new TestAutomapper()).TestRecord();
            var testContext = new TestContext();
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var repository = new EfRepository<InspRecord>(testContext);
            repository.Insert(record);
            sw.Stop();
            Console.WriteLine(string.Format ("WriteOver！ElapsedTime:{0}",sw.ElapsedMilliseconds));
            #endregion 写入

            #region 读
            sw.Restart();
            var querys = repository.Table.Where(x => x.PannelID.Equals("ABCDDDDDG"));
            sw.Stop();
            Console.WriteLine(string.Format ("ReadOver！querycount:{0}，ElapsedTime：{1}", querys .Count (), sw.ElapsedMilliseconds));
            sw.Restart();
            var query = querys.FirstOrDefault();
            InspRecordDTO dto = query.MapTo<InspRecordDTO>();
            sw.Stop();
            Console.WriteLine(string.Format("ConvertOver！ElapsedTime：{0}", sw.ElapsedMilliseconds));

            #endregion 读
            Console.Read();
        }
        static void FunLast()
        {
            var testContext = new TestContext();
            IRepository<InspRecord> repository = new EfRepository<InspRecord>(testContext);
            IRepository<InspGroupRecord> repository2 = new EfRepository<InspGroupRecord>(testContext);
            IRepository<Coordinate> repository3 = new EfRepository<Coordinate>(testContext);
#if false
            for (int i = 0; i < 5; i++)
            {
                var id = Guid.NewGuid().ToString().Replace("-", "");
                repository.Insert(new InspRecord()
                {
                    BeachID = BEACHID,
                    BeginTime = DateTime.Now,
                    ErrorCode = "ErrorCode",
                    ID = id,
                    InspTime = i * 10,
                    MachineID = MACHINEID,
                    ModelID = MODELID,
                    PanlID = string.Format("{0}{1}", PANELID, i),
                    Result = i % 2 == 0
                });
                for (int j = 0; j < i; j++)
                {
                    var cid = Guid.NewGuid().ToString().Replace("-", "");
                    repository2.Insert(new InspGroupRecord()
                    {
                        ID = cid,
                        ErrorMsg = "Msg",
                        ErrorType = "ErrorType",
                        GroupName = string.Format("{0}{1}", "Camera", i + 1),
                        GID = id,
                        ItemName = string.Format("图{0}", j + 1),
                        ItemResult = true
                    });
                    if (i % 2 == 0)
                        for (int k = 1; k < 4; k++)
                        {
                            repository3.Insert(new Coordinate()
                            {
                                CID = cid,
                                X = k * 2,
                                Y = k * 3,
                                Type = k,
                                Value = k * 10,
                                Diff = 9
                            });
                        }
                }
            }
            var insplist = repository.Table;
            foreach (var s in insplist)
            {
                Console.WriteLine(string.Format("{0} {1} ", s.BeginTime.ToShortTimeString(), s.PanlID));
                if (s.Groups == null || s.Groups.Count == 0)
                    continue;
                foreach (var g in s.Groups)
                {
                    Console.WriteLine(string.Format("{0} {1} ", g.GroupName, g.ItemName, s.PanlID));
                    if (g.CoordinateDetails == null || g.CoordinateDetails.Count == 0)
                        continue;
                    foreach (var point in g.CoordinateDetails)
                        Console.WriteLine(point.ToString());
                }
            }
            //var personList = repository.Table.Where(x => x.Name.Equals("程建林"));
            //foreach (var s in personList)
            //{
            //    Console.WriteLine(string.Format("{0} {1} {2}", s.ID, s.Name, s.Age));
            //    s.Age = "28";
            //    repository.Update(s);
            //}
            //IRepository<GroupResult> repository2 = new EfRepository<GroupResult>(testContext);
            //repository2.Insert(new GroupResult()
            //{
            //    ClassNo = 9,
            //    ArmyLevel = 1002,
            //    GID = Guid.NewGuid().ToString().Replace("-", ""),
            //    LocalAddr = "Russia"
            //});
            //var armList = repository2.Table;
            //foreach (var s in armList)
            //{
            //    Console.WriteLine(string.Format("{0} {1} {2}", s.GID, s.ArmyLevel, s.LocalAddr));
            //}
            Console.WriteLine("Over");
            Console.Read();
            // repository.GetById(
            ///获取数据库表Person中的所有数据 在查询的时候最好加上AsNoTracking 禁止EF跟踪
            //  var personList = testContext.Persons.AsNoTracking().ToList();
#endif
        }
    }
}
